package com.tianhai.db.parser;

import com.tianhai.db.parser.ast.*;

/**
 * SQL解析器测试类
 */
public class SqlParserTest {
    
    public static void main(String[] args) {
        System.out.println("========================================");
        System.out.println("    SQL解析器测试");
        System.out.println("========================================\n");
        
        SqlParser parser = new SqlParser();
        int totalTests = 0;
        int passedTests = 0;
        
        // 测试1: CREATE TABLE
        System.out.println("【测试1】CREATE TABLE语句");
        try {
            String sql = "CREATE TABLE users (id INT, name VARCHAR(50), age INT, email TEXT)";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof CreateTableStatement) {
                CreateTableStatement createStmt = (CreateTableStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + createStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试2: INSERT INTO
        System.out.println("【测试2】INSERT INTO语句");
        try {
            String sql = "INSERT INTO users (id, name, age, email) VALUES (1, 'Alice', 25, 'alice@example.com')";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof InsertStatement) {
                InsertStatement insertStmt = (InsertStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + insertStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试3: SELECT (全部列)
        System.out.println("【测试3】SELECT * 语句");
        try {
            String sql = "SELECT * FROM users";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof SelectStatement) {
                SelectStatement selectStmt = (SelectStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + selectStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试4: SELECT with WHERE
        System.out.println("【测试4】SELECT with WHERE语句");
        try {
            String sql = "SELECT id, name, age FROM users WHERE age >= 18 AND name = 'Alice'";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof SelectStatement) {
                SelectStatement selectStmt = (SelectStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + selectStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试5: SELECT with ORDER BY and LIMIT
        System.out.println("【测试5】SELECT with ORDER BY and LIMIT语句");
        try {
            String sql = "SELECT name, age FROM users WHERE age > 20 ORDER BY age DESC LIMIT 10";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof SelectStatement) {
                SelectStatement selectStmt = (SelectStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + selectStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试6: UPDATE
        System.out.println("【测试6】UPDATE语句");
        try {
            String sql = "UPDATE users SET age = 30, email = 'newemail@example.com' WHERE id = 1";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof UpdateStatement) {
                UpdateStatement updateStmt = (UpdateStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + updateStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试7: DELETE
        System.out.println("【测试7】DELETE语句");
        try {
            String sql = "DELETE FROM users WHERE age < 18";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof DeleteStatement) {
                DeleteStatement deleteStmt = (DeleteStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + deleteStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试8: BEGIN TRANSACTION
        System.out.println("【测试8】BEGIN TRANSACTION语句");
        try {
            String sql = "BEGIN TRANSACTION";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof BeginTransactionStatement) {
                BeginTransactionStatement beginStmt = (BeginTransactionStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + beginStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试9: BEGIN (简写)
        System.out.println("【测试9】BEGIN语句（简写）");
        try {
            String sql = "BEGIN;";
            Statement stmt = parser.parse(sql);
            
            if (stmt instanceof BeginTransactionStatement) {
                BeginTransactionStatement beginStmt = (BeginTransactionStatement) stmt;
                System.out.println("✓ 解析成功");
                System.out.println("  SQL: " + sql);
                System.out.println("  结果: " + beginStmt);
                passedTests++;
            } else {
                System.out.println("✗ 解析失败: 类型不匹配");
            }
        } catch (Exception e) {
            System.out.println("✗ 解析失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试总结
        System.out.println("========================================");
        System.out.println("           测试结果汇总");
        System.out.println("========================================");
        System.out.println("总测试数: " + totalTests);
        System.out.println("通过数: " + passedTests);
        System.out.println("失败数: " + (totalTests - passedTests));
        System.out.println("通过率: " + (passedTests * 100 / totalTests) + "%");
        System.out.println("----------------------------------------");
        
        if (passedTests == totalTests) {
            System.out.println("✓✓✓ 所有测试通过！");
        } else {
            System.out.println("✗✗✗ 部分测试失败");
        }
        System.out.println("========================================");
    }
}
